package com.jiuli.liping.util;

import com.alibaba.fastjson.JSONObject;
import com.jiuli.liping.service.impl.WeiXinServiceImpl;
import com.jiuli.liping.vo.AccessToken;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    /**
     * 2.1 String ->Timestamp
     * 20151101095440
     */
    public static Timestamp StringToTimestamp(String date){
        String reg = "(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})";
        date = date.replaceAll(reg, "$1-$2-$3 $4:$5:$6");
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }

    public static String getTime(Timestamp timestamp) {
        Date currentTime = timestamp;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(currentTime);
        return dateString;
    }

    public static String getNowTime(){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String hehe = dateFormat.format( now );
        System.out.println(hehe);
        return hehe;
    }

    public static long getTimeDifference3(Timestamp formatTime1, Timestamp formatTime2) {
        SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
        long t1 = formatTime1.getTime();
        long t2 = formatTime2.getTime();
        long minutes = (t1-t2)/1000/60;
        return minutes;
    }

    public static String getTimeDay(Timestamp timestamp) {
        Date currentTime = timestamp;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = format.format(currentTime);
        return dateString;
    }

    public static boolean compare(String time1,String time2) {
        try {
            //如果想比较日期则写成"yyyy-MM-dd"就可以了
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            //将字符串形式的时间转化为Date类型的时间
            Date a = sdf.parse(time1);
            Date b = sdf.parse(time2);
            //Date类的一个方法，如果a早于b返回true，否则返回false
            if (a.before(b))
                return true;
            else
                return false;
        }catch (Exception e){
            System.out.println(e);
        }
        return false;
    }

    public static void main(String[] args) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 22:00:00");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
        System.out.println(df2.format(new Date()));// new Date()为获取当前系统时间

        System.out.println(DateUtils.compare(df2.format(new Date()),df.format(new Date())));
       /* WeiXinServiceImpl wx = new WeiXinServiceImpl();
        String token = wx.getToken();
        JSONObject json = JSONObject.parseObject(token);
        AccessToken accessToken = new AccessToken();
        accessToken.setAccess_token(json.getString("access_token"));
        //获得token    json串结束
        String urls = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+accessToken.getAccess_token();
        String param = "{" +
                "\"touser\" :\""+3846+"\" ," +
                "\"msgtype\" : \"text\", "+
                "\"agentid\" : 1000044, "+
                "\"text\" : { \"content\" : \"你的商城物品领用申请已通过。\" },"+
                "\"safe\" : 0, "+
                "\"enable_id_trans\" : 0, "+
                "\"enable_duplicate_check\" : 0, " +
                "}";
        String json1 = WeiXinUtils.getWeiXinApiPost(urls, param);*/
    }
}
